import type { AxiosInstance, AxiosResponse } from 'axios'
import axios from 'axios'

const baseURL = import.meta.env.VITE_BASE_API
console.log(baseURL, 'baseURL');
const service: AxiosInstance = axios.create({
  baseURL: baseURL,
  timeout: 5000,
  headers: { 'Content-Type': 'application/json;charset=UTF-8' }
})

//请求
service.interceptors.request.use((config) => {
  //这里可以设置token
  return config
})

//响应
service.interceptors.response.use(
  (response: AxiosResponse) => {
    const data = response.data
    if (data.code === 200) {
      return data
    }else {
      return Promise.reject(data)
    }
  },
  (err) => {
    // if(err.response.status === 500){
    //   console.log('服务器异常，请稍后再试')
    // }
    return Promise.reject(err.response)
  }
)

export default service


